package mylinkqueue;

public class MyQueue {
    static class ListNode{
        public ListNode prev;
        public ListNode next;
        public int val;

        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;   //头
    public ListNode last;   //尾

    public int usedSize;

    public boolean offer(int val){
        ListNode newH=new ListNode(val);
        if(head==null){
            head=newH;
            last=newH;
        }else {
            last.next=newH;
            newH.prev=last;
            last=last.next;
        }
        this.usedSize++;
        return true;
    }

    public int poll(){
        if(head==null){
            return -1;
            //yichang
        }
        int retVal=head.val;
        if(head.next==null){
            head=null;
            last=null;

            return retVal;
        }
        head = head.next;
        head.prev = null;
        usedSize--;
        return retVal;
    }

    public int peek(){
        if(head==null){
            return -1;
        }
        return head.val;

    }

    public boolean empty(){
        return head==null;
    }

    public int size(){
        return usedSize;
    }
}
